home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d18 / tpflex.arc / FLEX.DEM < prev    next >
Text File  |  1991-04-28  |  15KB  |  361 lines

  1. {
  2.  
  3.     flex.dem
  4.     4-25-90
  5.  
  6.     Copyright 1990
  7.     John W. Small
  8.     All rights reserved
  9.  
  10.     PSW / Power SoftWare
  11.     P.O. Box 10072
  12.     McLean, Virginia 22102 8072
  13.     (703) 759-3838
  14.  
  15. }
  16.  
  17. program flexDemo;
  18.  
  19.     uses crt, flex;
  20.  
  21.     const
  22.  
  23.         iarray : array[1..10] of integer = (
  24.             1,2,3,4,5,6,7,8,9,10);
  25.  
  26.     type
  27.  
  28.         iarrayType = array[1..maxInt] of integer;
  29.  
  30.     var Q, S: FlexList;
  31.         i, r : integer;
  32.         iptr : ^integer;
  33.         line : string;
  34.         iaptr : ^iarrayType;
  35.  
  36.     {$F+}
  37.     function CompareIntegers(var buf1, buf2) : integer; {$F-}
  38.         var i1 : integer absolute buf1;
  39.             i2 : integer absolute buf2;
  40.         begin
  41.             if i1 < i2 then
  42.                 CompareIntegers := -1
  43.             else if i1 > i2 then
  44.                 CompareIntegers := 1
  45.             else
  46.                 CompareIntegers := 0
  47.         end;
  48.  
  49.     begin
  50.         clrscr;
  51.         writeln('It''s best to have a printed copy of flex.pas to read');
  52.         writeln('along with this demo.  Press ''Enter'' to continue.');
  53.         readln;
  54.         clrscr;
  55.         writeln('Anytime your Turbo Pascal 5.5 application requires a');
  56.         writeln('stack, queue, or linked list, simply include "flex"');
  57.         writeln('in its "uses" clause.  Next declare your stack, queue,');
  58.         writeln('or list as a variable of type "FlexList".  FlexList');
  59.         writeln('is really a generic hybrid stack-queue-list-array');
  60.         writeln('data structure.  Your FlexList variable can be');
  61.         writeln('initialized to store any type of data.');
  62.         writeln;
  63.         writeln('For example:');
  64.         writeln;
  65.         writeln('    var Q : FlexList;');
  66.         writeln('        i, r : integer;');
  67.         writeln;
  68.         writeln('    begin');
  69.         writeln('        Q.init(sizeof(r));');
  70.         writeln;
  71.         writeln('In the above example, r could have been any type, record');
  72.         writeln('or object, with any number of fields of any type.');
  73.         writeln;writeln('Press ''Enter'' to continue ... ');
  74.         readln;
  75.         clrscr;
  76.         writeln('The FlexList object has over 30 methods for accessing your');
  77.         writeln('"list" as a stack, queue, list, or an array');
  78.         writeln('interchangeably!  You can push, pop, insert, delete,');
  79.         writeln('sort, store, recall, or find, to name but just a few');
  80.         writeln('operations.  The complement of FlexList methods allows you');
  81.         writeln('to access your list''s data by value (copy) or by');
  82.         writeln('reference (pointer), as well as to move nodes directly');
  83.         writeln('between lists.');
  84.         writeln;
  85.         writeln('Consider the following code segment.');
  86.         writeln;
  87.         writeln('    { Insert 10 random integers into a queue. }');
  88.         writeln;
  89.         writeln('        for i := 1 to 10 do begin');
  90.         writeln('            r := random(10000);');
  91.         writeln('            write(r:8);');
  92.         writeln('            Q.insQ(r)');
  93.         writeln('            end;');
  94.         writeln;
  95.         writeln('Press ''Enter'' to execute the above code segment.');
  96.         readln;
  97.         Q.init(sizeof(r));
  98.         for i := 1 to 10 do begin
  99.             r := random(10000);
  100.             write(r:8);
  101.             Q.insQ(r)
  102.             end;
  103.         writeln;
  104.         writeln('Press ''Enter'' to continue ... ');
  105.         readln;
  106.         clrscr;
  107.         writeln('Next, consider this code segment: ');
  108.         writeln;
  109.         writeln('    {$F+}');
  110.         writeln('    function CompareIntegers(var buf1, buf2) : integer; {$F-}');
  111.         writeln('        var i1 : integer absolute buf1; i2 : integer absolute buf2;');
  112.         writeln('        begin');
  113.         writeln('            if i1 < i2 then');
  114.         writeln('                CompareIntegers := -1');
  115.         writeln('            else if i1 > i2 then');
  116.         writeln('                CompareIntegers := 1');
  117.         writeln('            else');
  118.         writeln('                CompareIntegers := 0');
  119.         writeln('        end;');
  120.         writeln;
  121.         writeln('    { Sort the queue and pop the results. }');
  122.         writeln;
  123.         writeln('        Q.sort(CompareIntegers);  ');
  124.         writeln('        Q.pop(r);');
  125.         writeln('        while Q.ok do begin');
  126.         writeln('            write(r:8); Q.pop(r) end;');
  127.         writeln;
  128.         writeln('Press ''Enter'' again to execute this code.');
  129.         readln;
  130.         clrscr;
  131.         writeln('First the random integers again ... ');
  132.         writeln;
  133.         while Q.next(r) do
  134.               write(r:8);
  135.         writeln;
  136.         writeln('And the sorted results ... ');
  137.         writeln;
  138.         Q.sort(CompareIntegers);
  139.         Q.pop(r);
  140.         while Q.ok do begin
  141.             write(r:8);
  142.             Q.pop(r);
  143.             end;
  144.         writeln;
  145.         writeln;
  146.         writeln('FlexList methods that perform sorts or searches take a procedure');
  147.         writeln('type parameter.  This "pointer" is to the compare function which');
  148.         writeln('compares the data stored in FlexList nodes to determine the sorted');
  149.         writeln('ascending order.  Your compare function decides which fields to');
  150.         writeln('compare!');
  151.         writeln;
  152.         writeln('Press ''Enter'' to continue ... ');
  153.         readln;
  154.         clrscr;
  155.         writeln('All FlexList methods ending in ''D'' access the data in your');
  156.         writeln('list by pointer.  Again, consider the following code.');
  157.         writeln;
  158.         writeln('    var iptr : ^integer;');
  159.         writeln('    ...');
  160.         writeln('    for i := 1 to 10 do begin');
  161.         writeln('        write(i:8); Q.push(i) end;');
  162.         writeln('    while Q.nextD(pointer(iptr)) do');
  163.         writeln('        write(iptr^:8);');
  164.         writeln;
  165.         writeln('Press ''Enter'' to view the results.');
  166.         readln;
  167.         writeln('First the order of integers pushed onto the stack ...');
  168.         writeln;
  169.         for i := 1 to 10 do begin
  170.             write(i:8);
  171.             Q.push(i)
  172.             end;
  173.         writeln;
  174.         writeln('And then reading from the top of stack back ...');
  175.         writeln;
  176.         while Q.nextD(pointer(iptr)) do
  177.             write(iptr^:8);
  178.         writeln;
  179.         writeln;
  180.         writeln('Press ''Enter'' to continue ... ');
  181.         readln;
  182.         clrscr;
  183.         writeln('Let''s read across that last stack again but this time');
  184.         writeln('using FlexList''s array access methods!  And the code ...');
  185.         writeln;
  186.         writeln('    for i := 1 to 10 do begin');
  187.         writeln('        Q.recall(r,i); write(r:8) end;');
  188.         writeln;
  189.         writeln('And of course the results ...');
  190.         writeln;
  191.         for i := 1 to 10 do begin
  192.             Q.recall(r,i); write(r:8) end;
  193.         writeln;
  194.         writeln;
  195.         writeln('Press ''Enter'' to continue ... ');
  196.         readln;
  197.         clrscr;
  198.         writeln('All FlexList methods ending in ''N'' operate on FlexNodes instead of the');
  199.         writeln('data in the nodes.  These come in handy when you are writing queuing');
  200.         writeln('network simulations and the like, where you want to be constantly moving');
  201.         writeln('nodes between queues.  A multitasking OS simulation is a good example');
  202.         writeln('where you move PCBs (process control blocks) between the run, ready,');
  203.         writeln('blocked, and swapped out queues.  Let''s see, that last queue with 10');
  204.         writeln('integers is still in memory, we''ll work on that one.  Consider the');
  205.         writeln('following code ...');
  206.         writeln;
  207.         writeln('    var S : FlexList;');
  208.         writeln('    ...');
  209.         writeln('    S.init(sizeof(integer));');
  210.         writeln('    while S.ok do  S.pushN(Q.popN);');
  211.         writeln('    while S.Prev(i) do  write(i:8);');
  212.         writeln('    Q.done; S.done;');
  213.         writeln;
  214.         writeln('And the results ...');
  215.         writeln;
  216.         S.init(sizeof(integer));
  217.         while S.ok do  S.pushN(Q.popN);
  218.         while S.Prev(i) do  write(i:8);
  219.         Q.done; S.done;
  220.         writeln;
  221.         writeln('Wow! We popped one stack into the other.  That should have reversed the');
  222.         writeln('order.  But wait, we then read the list backward with the prev() method.');
  223.         writeln('That''s right, everything''s okay.  Press ''Enter'' to continue ... ');
  224.         readln;
  225.         clrscr;
  226.         writeln('You may have noticed Q.done and S.done in the last example.  Whenever you');
  227.         writeln('are done with a FlexList your should call this method to cleanup.  If you were');
  228.         writeln('really watching closely during the last few examples, you probably wondered');
  229.         writeln('about how prev() and nextD() knew where in the list they were.  All FlexLists');
  230.         writeln('maintain a pointer to the current node.  Stack and queue methods don''t');
  231.         writeln('disturb this setting.  If the current is popped, however, the current node');
  232.         writeln('becomes undefined just as it is after FlexList initialization.  Next and prev');
  233.         writeln('"walk" the current pointer across the list, making the current become');
  234.         writeln('undefined once each cycle at which time these methods return false.  This');
  235.         writeln('is how I was able to control looping through the lists in the previous');
  236.         writeln('examples.  Array access methods, i.e. recall and store, set the current');
  237.         writeln('pointer to the last node accessed.  The next array access first determines');
  238.         writeln('whether the front, current, or rear pointer is closest to the requested');
  239.         writeln('node and then traverses from the closest pointer across the links to the');
  240.         writeln('requested node, making it the new current node.  Both methods call the');
  241.         writeln('mkcur method to perform this operation.  FlexList methods, get and put, also');
  242.         writeln('work with the current node.  The insert method inserts after the current');
  243.         writeln('node making the new node current, while del, deletes the current node');
  244.         writeln('making the previous node current.');
  245.         writeln;
  246.         writeln('Press ''Enter'' to continue ...');
  247.         readln;
  248.         clrscr;
  249.         writeln('Let''s see insert and del in action and this time with something other');
  250.         writeln('than integers!  Consider the following code and then start your input.');
  251.         writeln;
  252.         writeln('    Q.init(sizeof(line));');
  253.         writeln('    while Q.nodes < 3 do begin');
  254.         writeln('        write(''Enter String : ''); readln(line); Q.ins(line) end;');
  255.         writeln;
  256.         Q.init(sizeof(line));
  257.         while Q.nodes < 3 do begin
  258.             write('Enter String : '); readln(line); Q.ins(line) end;
  259.         writeln;
  260.         writeln('The last node is now the current one and we''ll start deleting them.');
  261.         writeln('Since deleting makes the previous node the new current node, successive');
  262.         writeln('deletes will walk across the list from the rear to the front.');
  263.         writeln('The following code will now be executed.');
  264.         writeln;
  265.         writeln('    Q.del(line);');
  266.         writeln('    while Q.ok do begin writeln(line); Q.del(line) end;');
  267.         writeln;
  268.         Q.del(line);
  269.         while Q.ok do begin writeln(line); Q.del(line) end;
  270.         writeln;
  271.         writeln('Press ''Enter'' to continue ...');
  272.         readln;
  273.         clrscr;
  274.         writeln('Sometimes you want to work with a list, other times it''s more convenient');
  275.         writeln('to work with an array.  Although Flexlist allows this chameleon behavior,');
  276.         writeln('your application may progress in stages that favor a list at one point');
  277.         writeln('and an array at another.  The FlexList object has methods for converting a');
  278.         writeln('conventional array into a FlexList object or a FlexList object into a dynamic');
  279.         writeln('array thus allowing you to optimize the performance of your application.');
  280.         writeln('You can think of the FlexList method, unpack, as exploding a conventional');
  281.         writeln('array into a FlexList.  Consider the following example.');
  282.         writeln;
  283.         writeln('    const iarray : array[1..10] of integer = (1,2,3,4,5,6,7,8,9,10);');
  284.         writeln('    ...');
  285.         writeln('    Q.unpack(sizeof(integer),10,iarray);');
  286.         writeln('    while Q.next(i) do write(i:8);');
  287.         writeln;
  288.         writeln('And the results ...');
  289.         writeln;
  290.         Q.unpack(sizeof(integer),10,iarray);
  291.         while Q.next(i) do write(i:8);
  292.         writeln;
  293.         writeln('Press ''Enter'' to continue ...');
  294.         readln;
  295.         clrscr;
  296.         writeln('Think of the FlexList method, pack, as imploding a FlexList into a dynamic');
  297.         writeln('array.  Consider the following code and the previous list of integers.');
  298.         writeln;
  299.         writeln('    type  iarrayType = array[1..maxInt] of integer;');
  300.         writeln('    ...');
  301.         writeln('    iaptr := Q.pack;');
  302.         writeln('    for i := 1 to Q.nodes do');
  303.         writeln('        write(iaptr^[i]:8);');
  304.         writeln;
  305.         writeln('And the results of its execution ...');
  306.         writeln;
  307.         iaptr := Q.pack;
  308.         for i := 1 to Q.nodes do
  309.             write(iaptr^[i]:8);
  310.         writeln;
  311.         writeln;
  312.         writeln('The FlexList method, packPtrs, simply creates a nil terminated array of');
  313.         writeln('packed pointers which point to the data areas of all the FlexList nodes.');
  314.         writeln('Do you remember how FlexList methods ending in ''D'' worked with pointers?');
  315.         writeln('These are the same pointers all packed into a dynamic array.  You can');
  316.         writeln('quickly zip around a FlexList''s nodes using these pointers to modify data.');
  317.         writeln('When your application is finished this phase of processing it discards');
  318.         writeln('the array of pointers.');
  319.         writeln;
  320.         writeln('Press ''Enter'' to continue ...');
  321.         readln;
  322.         clrscr;
  323.         writeln('Since FlexList is an object, you can derive your own objects from FlexList.');
  324.         writeln('Why would you want too?  Suppose that you need a place to store data');
  325.         writeln('pertaining to the whole list?  You can declare a new object, derived');
  326.         writeln('from FlexList, that contains variables for this data.  Thus you can store');
  327.         writeln('data associated with the list, in the list!');
  328.         writeln;
  329.         writeln('Your application''s code size won''t continue to grow when you add new');
  330.         writeln('types of lists either, since the FlexList object is generic, able to store');
  331.         writeln('any type of data, record or object.  Since FlexLists are initialized at');
  332.         writeln('run time, the data they hold or even their creation can be specified');
  333.         writeln('at run time thus allowing your application new dimensions of adaptibility');
  334.         writeln('to user inputs.  And with FlexList you can quickly construct');
  335.         writeln('lists of lists or other composite structures.  FlexList will save you');
  336.         writeln('hours of coding time, code space, money and headaches!');
  337.         writeln;
  338.         writeln('Press ''Enter'' to continue ...');
  339.         readln;
  340.         clrscr;
  341.         writeln('This demo, flex.int, and flex.tpu are offered as shareware, meaning try');
  342.         writeln('before you buy.  If you find FlexList useful and are using it in your');
  343.         writeln('applications, a registration fee of $20 is required.  Upon registration');
  344.         writeln('you will be sent manual, source code and the latest examples');
  345.         writeln('programs on a DOS formatted 5 1/4" disk.');
  346.         writeln;
  347.         writeln('Copyright 1990, John W. Small, All right reserved');
  348.         writeln;
  349.         writeln('PSW / Power SoftWare');
  350.         writeln('P.O. Box 10072');
  351.         writeln('McLean, Virginia 22102 8072');
  352.         writeln('(703) 759-3838');
  353.         writeln;
  354.         writeln('PolyList, a close cousin of FlexList with nodes that are polymorhpic');
  355.         writeln('objects, is now available for Turbo Pascal 5.5.  Call or write or');
  356.         writeln('check your favorite BBS for details.');
  357.         writeln;
  358.         writeln('Press enter to quit.');
  359.         readln;
  360.     end.
  361.